#include<bits/stdc++.h>
using namespace std;

const long long MAXN=523333;

class Plan
{
	public:
		int a,b,t;
		bool  operator < (const Plan &p) const
		{
			if(this->a==p.a)
			{
				if(this->b==p.b)
				{
					return this->t < p.t;
				}
				else return this->b <p.b;
				
			}else return this->a <p.a;
			
		}
		bool const operator <= (const Plan &p)const
		{
			if(this->a==p.a)
			{
				if(this->b==p.b)
				{
					return this->t <= p.t;
				}else return this->b <= p.b;
				
			}else this->a <= p.a;
			
		}
		void show()
		{
			cout<<"from :"<<a<<" to: "<<b<<" time: "<<t<<endl; 
		}
};

class cmp
{
	public:
		bool operator ()(const Plan &p1,const Plan &p2) const
		{
			if(p1.a==p2.a)
			{
				if(p1.b==p2.b)
				{
					return p1.t < p2.t;
				}else return p1.b < p2.b;
				
			}else return p1.a < p2.a;
		}
};



multiset<Plan> ms;
Plan plans[MAXN];


int main()
{
	cin.sync_with_stdio(false);
	int n;cin>>n;
	for(int i=0;i<n;++i)
	{
		Plan p1;
		cin>>p1.a>>p1.b>>p1.t;
		plans[i]=p1;
		ms.insert(p1);
	}
	for(int i=0;i<n;++i)
	{
		multiset<Plan>::iterator it1=ms.lower_bound(plans[i]);
		Plan pp=*it1;
		
		plans[i].show();pp.show();
	}
	cout<<endl;
	for(int i=0;i<n;++i)
	{
		if(i&&plans[i]<plans[i-1])plans[i].show();
	}
	
	
	return 0;
}
